package com.py.credit.controller;

import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Column;
import com.py.credit.service.BatchLogService;
import com.py.credit.service.XypjInspectDataService;
import com.py.credit.service.excel.read.service.InspectDataStyleService;
import com.py.framework.core.annotation.IgnoreAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 信用评价用户校验
 *
 * @author YaoS
 * @date 19/4/3 11:02
 */
@RestController
@RequestMapping(value = "/xypjInspect")
public class XypjInspectDataController {
    @Autowired
    private XypjInspectDataService xypjInspectDataService;

    @Autowired
    @Qualifier("xypj")
    private InspectDataStyleService inspectDataStyleService;

    @Autowired
    private BatchLogService batchLogService;


    /**
     * 字段校验
     *
     * @param value   参数值
     * @param tableId 表ID
     * @param type    字段类型
     * @param code    字段ID
     * @return
     * @author YaoS
     * @date 16:16 18/11/3
     **/
    @IgnoreAuthority
    @RequestMapping(value = "/xypjInspect.json")
    public RestServiceResult xypjInspect(
            @RequestParam(value = "value", defaultValue = "", required = true) String value,
            @RequestParam(value = "tableId", defaultValue = "", required = true) String tableId,
            @RequestParam(value = "type", defaultValue = "", required = true) String type,
            @RequestParam(value = "code", defaultValue = "", required = true) String code,
            HttpServletRequest request) {
        RestServiceResult result = new RestServiceResult<>();
        try {
            Map map = xypjInspectDataService.xypjCkeck(value, tableId, type, code);
            result.setData(map);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("规则校验异常！");
            ExceptionLogger.error("规则校验异常！", e.getMessage(), e);
            return result;
        }
    }

    /**
     * 表校验
     *
     * @param tableId
     * @param list
     * @return RestServiceResult
     * @author YaoS
     * @date 16:17 18/11/3
     **/
    @IgnoreAuthority
    @RequestMapping(value = "/xypjTableInspect.json")
    public RestServiceResult xypjTableInspect(
            @RequestParam(value = "tableId", defaultValue = "", required = true) Integer tableId,
            @RequestParam(value = "list", defaultValue = "", required = true) String list,
            HttpServletRequest request) {
        RestServiceResult result = new RestServiceResult<>();
        ArrayList<Map<String, Object>> checkList = JSONArray.parseObject(list, ArrayList.class);
        Map<String, Object> chackData = new HashMap<>();
        for (Map map : checkList) {
            chackData.put((String) map.get("key"), map.get("value"));
        }
        try {
            List<Column> columns = batchLogService.getColumnList(tableId);
            String sb = inspectDataStyleService.dataCheck(tableId, chackData, columns);
            result.setData(sb);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("规则校验异常！");
            ExceptionLogger.error("规则校验异常！", e.getMessage(), e);
            return result;
        }
    }
}
